package com.laowang.project.system.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import com.laowang.common.exception.CustomException;
import com.laowang.common.utils.DateUtils;
import com.laowang.common.utils.SecurityUtils;
import com.laowang.project.system.domain.*;
import com.laowang.project.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.laowang.project.system.mapper.MyCyhWithdrawMapper;

import javax.validation.constraints.NotNull;

/**
 * 用户申请提现Service业务层处理
 *
 * @author laowang
 * @date 2023-06-09
 */
@Service
public class MyCyhWithdrawServiceImpl implements MyCyhWithdrawService {
    @Autowired
    private MyCyhWithdrawMapper myCyhWithdrawMapper;

    @Autowired
    private WxUserService wxUserService;

    @Autowired
    private MyCarBookService myCarBookService;

    @Autowired
    private UserGoldLogService userGoldLogService;

    /**
     * 查询用户申请提现
     *
     * @param id 用户申请提现ID
     * @return 用户申请提现
     */
    @Override
    public MyCyhWithdraw selectMyCyhWithdrawById(Integer id) {
        return myCyhWithdrawMapper.selectMyCyhWithdrawById(id);
    }

    /**
     * 查询用户申请提现列表
     *
     * @param myCyhWithdraw 用户申请提现
     * @return 用户申请提现
     */
    @Override
    public List<MyCyhWithdraw> selectMyCyhWithdrawList(MyCyhWithdraw myCyhWithdraw) {
        return myCyhWithdrawMapper.selectMyCyhWithdrawList(myCyhWithdraw);
    }

    /**
     * 新增用户申请提现
     *
     * @param myCyhWithdraw 用户申请提现
     * @return 结果
     */
    @Override
    public int insertMyCyhWithdraw(MyCyhWithdraw myCyhWithdraw) {
        myCyhWithdraw.setCreateTime(DateUtils.getNowDate());
        return myCyhWithdrawMapper.insertMyCyhWithdraw(myCyhWithdraw);
    }

    /**
     * 修改用户申请提现
     *
     * @param myCyhWithdraw 用户申请提现
     * @return 结果
     */
    @Override
    public int updateMyCyhWithdraw(MyCyhWithdraw myCyhWithdraw) {
        Integer status = myCyhWithdraw.getStatus();
        MyCyhWithdraw myCyhWithdraw1 = selectMyCyhWithdrawById(myCyhWithdraw.getId());
        if (myCyhWithdraw1.getStatus().equals(1)) {
            throw new CustomException("用户已经提现", 400);
        }
        if (status.equals(1)) {
            WxUser wxUser = wxUserService.selectWxUserById(myCyhWithdraw.getUserId());
            // 冻结的金额减去此次冻结的金额
            Integer frozenGold = wxUser.getFrozenGold();
            Integer remain = frozenGold - myCyhWithdraw.getGold();
            wxUser.setFrozenGold(remain);
            wxUserService.updateWxUser(wxUser);

        }
        return myCyhWithdrawMapper.updateMyCyhWithdraw(myCyhWithdraw);
    }

    /**
     * 批量删除用户申请提现
     *
     * @param ids 需要删除的用户申请提现ID
     * @return 结果
     */
    @Override
    public int deleteMyCyhWithdrawByIds(Integer[] ids) {
        return myCyhWithdrawMapper.deleteMyCyhWithdrawByIds(ids);
    }

    /**
     * 删除用户申请提现信息
     *
     * @param id 用户申请提现ID
     * @return 结果
     */
    @Override
    public int deleteMyCyhWithdrawById(Integer id) {
        return myCyhWithdrawMapper.deleteMyCyhWithdrawById(id);
    }

    @Override
    public synchronized void withdrawUser(MyCyhWithdraw myCyhWithdraw) {
        Integer mid = SecurityUtils.getLoginUser().getWxUser().getMid();


        @NotNull(message = "金币不能为空") Integer gold = myCyhWithdraw.getGold();
        WxUser wxUser = wxUserService.selectWxUserById(mid);


        if (wxUser.getPromoDor() != null && wxUser.getPromoDor().equals(1) && wxUser.getExpireTime1() != null && wxUser.getExpireTime1().after(new Date())) {

        } else {
            ////不是推荐管
            if (wxUser.getIsPartner() != null && wxUser.getIsPartner().equals(1) && wxUser.getExpireTime2() != null && wxUser.getExpireTime2().after(new Date())) {

            } else {
                //不是合伙人

                if (wxUser.getStockHolder() != null && wxUser.getStockHolder().equals(1)) {

                } else {
                    //不是股东

                    if (wxUser.getVipLevel() != null && wxUser.getExpireTime() != null && wxUser.getExpireTime().after(new Date())) {
                        MyCarBook myCarBook = myCarBookService.selectMyCarBookByUserId(mid);
                        if (myCarBook == null || myCarBook.getIsAuth().equals("0")) {
                            throw new CustomException("用户没有认证不能申请提现");
                        }

                    } else {
                        //不是会员
                        if (wxUser.getVipLevel() == null) {
                            throw new CustomException("非车友会等级头衔不能提现");
                        }
                    }


                }
            }

        }


        Integer goldCoin = wxUser.getGoldCoin();
        if (goldCoin >= gold) {
            Integer remainGold = goldCoin - gold;
            myCyhWithdraw.setUserId(mid);
            myCyhWithdraw.setStatus(0);
            myCyhWithdraw.setCreateTime(new Date());
            myCyhWithdraw.setGold(gold);
            insertMyCyhWithdraw(myCyhWithdraw);
            //更新用户金币信息
            Integer frozenGold = wxUser.getFrozenGold();
            wxUser.setFrozenGold(frozenGold + gold);
            wxUser.setGoldCoin(remainGold);
            wxUserService.updateWxUser(wxUser);

            //金币日志
            UserGoldLog userGoldLog = new UserGoldLog();
            userGoldLog.setOldBalance(new BigDecimal(goldCoin));
            userGoldLog.setChangeBalance(new BigDecimal(gold).negate());
            userGoldLog.setNewBalance(new BigDecimal(remainGold));
            userGoldLog.setType(1);//变动类型，1提现，2活动佣金
            userGoldLog.setCreated(new Date());
            userGoldLog.setUserId(mid);
            userGoldLog.setRemark("提现");
            //userGoldLog.setRemark(String.valueOf(myCyhActivitySign.getActivityId()) );//活动id
            //userGoldLog.setFromSourceId(String.valueOf(myCyhActivitySign.getUserId()) );//谁买的
            userGoldLogService.insertUserGoldLog(userGoldLog);
        } else {
            throw new CustomException("操作金币有误", 400);
        }


    }
}
